package xray

import (
	"fmt"
	"gitee.com/gam6it/SecToolkit/utils/http2"
	"time"
)

const (
	XProcessStatusFinished = "FINISHED"
)

type XProcess struct {
	client     *Client
	Id         int    `json:"id"`
	Uuid       string `json:"uuid"`
	PlanId     int    `json:"plan_id"`
	TaskTarget struct {
		Target     []string `json:"target"`
		TargetType string   `json:"target_type"`
	} `json:"task_target"`
	BasicSetting                   BasicSetting `json:"basic_setting"`
	TaskSetting                    TaskSetting  `json:"task_setting"`
	CreatedTime                    time.Time    `json:"created_time"`
	StartTime                      time.Time    `json:"start_time"`
	EndTime                        time.Time    `json:"end_time"`
	Status                         string       `json:"status"`
	Logs                           []string     `json:"logs"`
	Timeout                        bool         `json:"timeout"`
	VulnResultSum                  int          `json:"vuln_result_sum"`
	VulnResultSeverityDistribution []struct {
		Severity string `json:"severity"`
		Count    int    `json:"count"`
	} `json:"vuln_result_severity_distribution"`
}

type XProcessProgress struct {
	Progress int    `json:"progress"`
	Status   string `json:"status"`
}

func newXProcess(c *Client, id int) (*XProcess, error) {
	var api = fmt.Sprintf("%sapi/v2/xprocess/%d/", c.URI, id)
	var xp XProcess
	err := request(http2.MethodGet, api, c.Token, &xp, http2.TimeOut(c.TimeOut))
	xp.client = c
	return &xp, err
}

func (xp *XProcess) Progress() (XProcessProgress, error) {
	var api = fmt.Sprintf("%sapi/v2/xprocess/%d/progress/", xp.client.URI, xp.Id)
	var xpp XProcessProgress
	err := request(http2.MethodGet, api, xp.client.Token, &xpp, http2.TimeOut(xp.client.TimeOut))
	return xpp, err
}
